package com.example.asus.shixun2;

import android.Manifest;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatSeekBar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.SeekBar;

import com.example.asus.shixun2.control.GameControl;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener{

    //View中不存放数据，没有变量
    View view_game;//游戏区域控件
    View view_score;//分数栏控件

    int score = 0;//分数
    public Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            String str = (String)msg.obj;
            switch(str){
                case "invalidate" :
                    //执行重绘
                    view_game.invalidate();
                    view_score.invalidate();
                    break;
                case "Pause" :
                    pause.setBackgroundResource(R.drawable.pause2);
                    break;
                case "Continue" :
                    pause.setBackgroundResource(R.drawable.pause1);
                    break;
                case "No_Sound" :
                    sound.setBackgroundResource(R.drawable.off);
                    break;
                case "Yes_Sound" :
                    sound.setBackgroundResource(R.drawable.on);
                    break;
                case "No_Music" :
                    music.setBackgroundResource(R.drawable.off);
                    break;
                case "Yes_Music" :
                    music.setBackgroundResource(R.drawable.on);
                    break;
            }
        }
    };

    private AppCompatButton pause;//暂停、继续按钮
    private AppCompatButton music;//背景音乐按钮
    private AppCompatButton sound;//背景音效按钮
    AppCompatSeekBar seekBar;



    //游戏控制器
    GameControl gameControl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setTitle("俄罗斯方块");
        if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);	//无title
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main2);

        pause = (AppCompatButton)findViewById(R.id.bt_pause);
        music = (AppCompatButton)findViewById(R.id.bt_music);
        sound = (AppCompatButton)findViewById(R.id.bt_sound);
        seekBar = (AppCompatSeekBar)findViewById(R.id.seek_bar);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                pause.getBackground().setAlpha(progress);//设置组件透明度
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        //实例化游戏控制器
        gameControl = new GameControl(handler,this);
        initView();
        initListener();

        if (ContextCompat.checkSelfPermission(Main2Activity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(Main2Activity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            gameControl.initMediaPlayer(this);//初始化播放器 MediaPlayer
        }
    }

    /**初始化监听*/
    private void initListener() {
        findViewById(R.id.bt_left).setOnClickListener(this);
        findViewById(R.id.bt_right).setOnClickListener(this);
        findViewById(R.id.bt_up).setOnClickListener(this);
        findViewById(R.id.bt_down).setOnClickListener(this);
        findViewById(R.id.bt_start).setOnClickListener(this);
        findViewById(R.id.bt_pause).setOnClickListener(this);
        findViewById(R.id.bt_music).setOnClickListener(this);
        findViewById(R.id.bt_sound).setOnClickListener(this);
    }

    /**初始化视图*/
    private void initView() {

        //得到游戏区域父容器
        FrameLayout layoutGame = (FrameLayout)findViewById(R.id.layoutGame);
        //实例化游戏区域
        view_game  = new View(this){
            //重写游戏区域的绘制方法
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);

                //绘制
                gameControl.draw_game(canvas);
            }
        };
        //设置游戏区域背景色
//        view_game.setBackgroundColor(Color.BLACK);
        //设置游戏区域大小
        view_game.setLayoutParams(new ActionBar.LayoutParams(Tool.xWidth,Tool.xHeight));
        //添加到游戏区域父容器
        layoutGame.addView(view_game);
        //得到分数兰区域父容器
        FrameLayout layoutScore = (FrameLayout)findViewById(R.id.layout_score);
        //实例化分数栏区域
        view_score = new View(this){
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                gameControl.draw_score(canvas);
            }
        };
        //设置分数栏区域背景色
        view_score.setBackgroundColor(Color.GRAY);
        //设置分数栏区域大小
        view_score.setLayoutParams(new ActionBar.LayoutParams(Tool.sWidth - 35,Tool.sHeight + 50));
        //添加到游戏区域父容器
        layoutScore.addView(view_score);
    }

    /**捕捉点击事件*/
    @Override
    public void onClick(View v) {
        gameControl.onClick(v.getId());
        view_game.invalidate();//调用该方法重新绘制，相当于Java的repaint
//        is_left_right = false;
    }
    @Override
    protected void onPause() {
        super.onPause();
        gameControl.pauseMusic();
    }

}

